package com.scnb.prototype.v1;

/**
 * 浅克隆
 */

public class Test {

    public static void main(String[] args) throws CloneNotSupportedException {
        Person p1 = new Person();
        Person p2 = (Person)p1.clone();
        System.out.println(p2.age + " " + p2.score);
        System.out.println(p2.loc);

        System.out.println("p1.loc == p2.loc ? " + (p1.loc == p2.loc));
        p1.loc.street = "sh";
        System.out.println(p2.loc);
    }
}


//实现标记型接口 Cloneable，才能实现类的克隆
class Person implements Cloneable {
    int age = 8;
    int score = 100;
    Location loc = new Location("北京", "22");

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

class Location{
    String street;
    String roomNo;

    public Location(String street, String roomNo) {
        this.street = street;
        this.roomNo = roomNo;
    }

    @Override
    public String toString() {
        return "Location:[" +
                "street = " + street +
                ", roomNo = " + roomNo +
                "]";
    }
}